package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * rabbitmq的配置项
 * Created by veblen on 2016/3/6.
 */
@Slf4j
@Data
public class RabbitConfig {

    /**
     * properties的配置数据
     */
    public static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("rabbitmq");

    public static String getString(String key) {
        return RESOURCE_BUNDLE.getString(key);
    }


    public static String host = getString("spring.rabbitmq.host");
    public static int port = Integer.parseInt(getString("spring.rabbitmq.port"));
    public static String username = getString("spring.rabbitmq.username");
    public static String password = getString("spring.rabbitmq.password");
    public static String vhost = getString("spring.rabbitmq.vhost");
    public static String queueExchange = getString("spring.rabbitmq.queue.exchange");
    public static String delayQueueExchange = getString("spring.rabbitmq.queue.delay.exchange");
    public static int qos = Integer.parseInt(getString("spring.rabbitmq.queue.qos"));
    public static String queue = getString("spring.rabbitmq.queue_key");
    public static String routingKey = getString("spring.rabbitmq.routingKey");
    public static boolean confirms = Boolean.parseBoolean(getString("spring.rabbitmq.publisher-confirms"));
    public static boolean returns = Boolean.parseBoolean(getString("spring.rabbitmq.publisher-returns"));
    public static boolean mandatory = Boolean.parseBoolean(getString("spring.rabbitmq.template.mandatory"));

    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("rabbitmq")
    public void redisChange(ConfigChangeEvent changeEvent){
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("spring.rabbitmq.host".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("spring.rabbitmq.port".equals(change.getPropertyName())) {
                port = Integer.valueOf(change.getNewValue());
            }
            if ("spring.rabbitmq.username".equals(change.getPropertyName())) {
                username = change.getNewValue();
            }
            if ("spring.rabbitmq.password".equals(change.getPropertyName())) {
                password = change.getNewValue();
            }
            if ("spring.rabbitmq.vhost".equals(change.getPropertyName())) {
                vhost = change.getNewValue();
            }
            if ("spring.rabbitmq.queue.exchange".equals(change.getPropertyName())) {
                queueExchange = change.getNewValue();
            }
            if ("spring.rabbitmq.queue.delay.exchange".equals(change.getPropertyName())) {
                delayQueueExchange = change.getNewValue();
            }
            if ("spring.rabbitmq.queue.qos".equals(change.getPropertyName())) {
                qos = Integer.valueOf(change.getNewValue());
            }
            if ("spring.rabbitmq.queue_key".equals(change.getPropertyName())) {
                queue = change.getNewValue();
            }
            if ("spring.rabbitmq.routingKey".equals(change.getPropertyName())) {
                routingKey = change.getNewValue();
            }
            if ("spring.rabbitmq.publisher-confirms".equals(change.getPropertyName())) {
                confirms = Boolean.valueOf(change.getNewValue());
            }
            if ("spring.rabbitmq.publisher-returns".equals(change.getPropertyName())) {
                returns = Boolean.valueOf(change.getNewValue());
            }
            if ("spring.rabbitmq.template.mandatory".equals(change.getPropertyName())) {
                mandatory = Boolean.valueOf(change.getNewValue());
            }
        }
    }
}
